package com.qzsoft.zhjd.server.modules.asphalt.handler;

import java.math.BigDecimal;
import java.math.RoundingMode;

import com.qzsoft.zhjd.server.common.utils.NumberUtils;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltDataWeightEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltMatchColdEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltMatchEntity;
import com.qzsoft.zhjd.server.modules.asphalt.model.CollectDataModel;

/**
 * @ClassName AsphaltDataWeightHandler
 * @Description 生产数据重量表处理
 * @author linhuawei
 * @since 2022-02-09
 */
public class AsphaltDataWeightHandler {

    /**
     * @Title coverTo
     * @Description 生产数据重量表处理
     * @param model 采集到的数据
     * @param match 生产配比
     * @param cold  冷料配比
     * @return 生产数据重量表数据
     */
    public static CollectAsphaltDataWeightEntity coverTo(CollectDataModel model, CollectAsphaltMatchEntity match,
                                                         CollectAsphaltMatchColdEntity cold) {
        CollectAsphaltDataWeightEntity weightData = new CollectAsphaltDataWeightEntity();

        // 实际用量
        weightData.setAsphaltAct(NumberUtils.setScale(model.getAsphaltAct(), 2));// 沥青实际用量
        weightData.setGravel1Act(NumberUtils.setScale(model.getGravel1Act(), 2));// 骨料1实际用量
        weightData.setGravel2Act(NumberUtils.setScale(model.getGravel2Act(), 2));// 骨料2实际用量
        weightData.setGravel3Act(NumberUtils.setScale(model.getGravel3Act(), 2));// 骨料3实际用量
        weightData.setGravel4Act(NumberUtils.setScale(model.getGravel4Act(), 2));// 骨料4实际用量
        weightData.setGravel5Act(NumberUtils.setScale(model.getGravel5Act(), 2));// 骨料5实际用量
        weightData.setGravel6Act(NumberUtils.setScale(model.getGravel6Act(), 2));// 骨料6实际用量
        weightData.setPowder1Act(NumberUtils.setScale(model.getPowder1Act(), 2));// 粉料1实际用量
        weightData.setPowder2Act(NumberUtils.setScale(model.getPowder2Act(), 2));// TODO 矿粉2用量
        weightData.setPowder3Act(NumberUtils.setScale(model.getPowder3Act(), 2));// TODO 矿粉3用量
        weightData.setRecycledMaterialAct(NumberUtils.setScale(model.getRecycledMaterialAct(), 2));// 再生料实际用量

        // 沥青之外的重量
        BigDecimal gravelWeight = BigDecimal.ZERO;
        gravelWeight = null != weightData.getGravel1Act() ? gravelWeight.add(weightData.getGravel1Act()) : gravelWeight;
        gravelWeight = null != weightData.getGravel2Act() ? gravelWeight.add(weightData.getGravel2Act()) : gravelWeight;
        gravelWeight = null != weightData.getGravel3Act() ? gravelWeight.add(weightData.getGravel3Act()) : gravelWeight;
        gravelWeight = null != weightData.getGravel4Act() ? gravelWeight.add(weightData.getGravel4Act()) : gravelWeight;
        gravelWeight = null != weightData.getGravel5Act() ? gravelWeight.add(weightData.getGravel5Act()) : gravelWeight;
        gravelWeight = null != weightData.getGravel6Act() ? gravelWeight.add(weightData.getGravel6Act()) : gravelWeight;
        gravelWeight = null != weightData.getPowder1Act() ? gravelWeight.add(weightData.getPowder1Act()) : gravelWeight;
        gravelWeight = null != weightData.getPowder2Act() ? gravelWeight.add(weightData.getPowder2Act()) : gravelWeight;
        gravelWeight = null != weightData.getPowder3Act() ? gravelWeight.add(weightData.getPowder3Act()) : gravelWeight;
        gravelWeight = null != weightData.getRecycledMaterialAct()
                ? gravelWeight.add(weightData.getRecycledMaterialAct())
                : gravelWeight;
        weightData.setGravelWeight(gravelWeight);

        // 加上沥青的重量
        BigDecimal totalWeight = null == weightData.getAsphaltAct() ? gravelWeight
                : gravelWeight.add(weightData.getAsphaltAct());
        weightData.setTotalWeight(totalWeight);

        // 理论用量
        if (null == match) {
            weightData.setAsphaltTheo(NumberUtils.setScale(model.getAsphaltTheo(), 2));// 沥青实际用量
            weightData.setGravel1Theo(NumberUtils.setScale(model.getGravel1Theo(), 2));// 骨料1实际用量
            weightData.setGravel2Theo(NumberUtils.setScale(model.getGravel2Theo(), 2));// 骨料2实际用量
            weightData.setGravel3Theo(NumberUtils.setScale(model.getGravel3Theo(), 2));// 骨料3实际用量
            weightData.setGravel4Theo(NumberUtils.setScale(model.getGravel4Theo(), 2));// 骨料4实际用量
            weightData.setGravel5Theo(NumberUtils.setScale(model.getGravel5Theo(), 2));// 骨料5实际用量
            weightData.setGravel6Theo(NumberUtils.setScale(model.getGravel6Theo(), 2));// 骨料6实际用量
            weightData.setPowder1Theo(NumberUtils.setScale(model.getPowder1Theo(), 2));// 粉料1实际用量
            weightData.setPowder2Theo(NumberUtils.setScale(model.getPowder2Theo(), 2));// TODO 粉料2实际用量
            weightData.setPowder3Theo(NumberUtils.setScale(model.getPowder3Theo(), 2));// TODO 粉料3实际用量
            weightData.setRecycledMaterialTheo(NumberUtils.setScale(model.getRecycledMaterialTheo(), 2));// 再生料实际用量
        } else {
            // 设置理论重量（使用外掺法）
            weightData.setAsphaltTheo(countWeight(match.getAsphaltAggregateRatio(), gravelWeight));// 沥青理论用量
            weightData.setGravel1Theo(countWeight(match.getGravel1(), gravelWeight));// 骨料1理论用量
            weightData.setGravel2Theo(countWeight(match.getGravel2(), gravelWeight));// 骨料2理论用量
            weightData.setGravel3Theo(countWeight(match.getGravel3(), gravelWeight));// 骨料3理论用量
            weightData.setGravel4Theo(countWeight(match.getGravel4(), gravelWeight));// 骨料4理论用量
            weightData.setGravel5Theo(countWeight(match.getGravel5(), gravelWeight));// 骨料5理论用量
            weightData.setGravel6Theo(countWeight(match.getGravel6(), gravelWeight));// 骨料6理论用量
            weightData.setRecycledMaterialTheo(countWeight(match.getRecycledMaterial(), gravelWeight));// 再生料理论用量
            weightData.setPowder1Theo(countWeight(match.getPowder1(), gravelWeight));// 矿粉理论用量
            weightData.setPowder2Theo(countWeight(match.getPowder2(), gravelWeight));// 矿粉理论用量
            weightData.setPowder3Theo(countWeight(match.getPowder3(), gravelWeight));// 矿粉理论用量
        }

        // 计算冷料用量
        if (null != cold) {
            weightData.setAsphaltCold(countWeight(cold.getAsphalt(), gravelWeight));// 沥青冷料用量
            weightData.setGravel1Cold(countWeight(cold.getGravel1(), gravelWeight));// 骨料1冷料用量
            weightData.setGravel2Cold(countWeight(cold.getGravel2(), gravelWeight));// 骨料2冷料用量
            weightData.setGravel3Cold(countWeight(cold.getGravel3(), gravelWeight));// 骨料3冷料用量
            weightData.setGravel4Cold(countWeight(cold.getGravel4(), gravelWeight));// 骨料4冷料用量
            weightData.setGravel5Cold(countWeight(cold.getGravel5(), gravelWeight));// 骨料5冷料用量
            weightData.setGravel6Cold(countWeight(cold.getGravel6(), gravelWeight));// 骨料6冷料用量
            weightData.setRecycledMaterialCold(countWeight(cold.getRecycledMaterial(), gravelWeight));// 再生料冷料用量
            weightData.setPowder1Cold(countWeight(cold.getPowder1(), gravelWeight));// 矿粉冷料用量
            weightData.setPowder2Cold(countWeight(cold.getPowder2(), gravelWeight));// 矿粉冷料用量
            weightData.setPowder3Cold(countWeight(cold.getPowder3(), gravelWeight));// 矿粉冷料用量
            weightData.setWoodFiberCold(countWeight(cold.getWoodFiber(), gravelWeight));// 木质纤维冷料用量
        }

        // 计算骨料、矿粉实际用量
        BigDecimal gravelAct = BigDecimal.ZERO;
        gravelAct = null != weightData.getGravel1Act() ? gravelAct.add(weightData.getGravel1Act()) : gravelAct;
        gravelAct = null != weightData.getGravel2Act() ? gravelAct.add(weightData.getGravel2Act()) : gravelAct;
        gravelAct = null != weightData.getGravel3Act() ? gravelAct.add(weightData.getGravel3Act()) : gravelAct;
        gravelAct = null != weightData.getGravel4Act() ? gravelAct.add(weightData.getGravel4Act()) : gravelAct;
        gravelAct = null != weightData.getGravel5Act() ? gravelAct.add(weightData.getGravel5Act()) : gravelAct;
        gravelAct = null != weightData.getGravel6Act() ? gravelAct.add(weightData.getGravel6Act()) : gravelAct;
        weightData.setGravelAct(gravelAct);

        BigDecimal powderAct = BigDecimal.ZERO;
        powderAct = null != weightData.getPowder1Act() ? powderAct.add(weightData.getPowder1Act()) : powderAct;
        powderAct = null != weightData.getPowder2Act() ? powderAct.add(weightData.getPowder2Act()) : powderAct;
        powderAct = null != weightData.getPowder3Act() ? powderAct.add(weightData.getPowder3Act()) : powderAct;
        weightData.setPowderAct(powderAct);

        // 计算骨料、矿粉理论用量
        BigDecimal gravelTheo = BigDecimal.ZERO;
        gravelTheo = null != weightData.getGravel1Theo() ? gravelAct.add(weightData.getGravel1Theo()) : gravelTheo;
        gravelTheo = null != weightData.getGravel2Theo() ? gravelAct.add(weightData.getGravel2Theo()) : gravelTheo;
        gravelTheo = null != weightData.getGravel3Theo() ? gravelAct.add(weightData.getGravel3Theo()) : gravelTheo;
        gravelTheo = null != weightData.getGravel4Theo() ? gravelAct.add(weightData.getGravel4Theo()) : gravelTheo;
        gravelTheo = null != weightData.getGravel5Theo() ? gravelAct.add(weightData.getGravel5Theo()) : gravelTheo;
        gravelTheo = null != weightData.getGravel6Theo() ? gravelAct.add(weightData.getGravel6Theo()) : gravelTheo;
        weightData.setGravelTheo(gravelTheo);

        BigDecimal powderTheo = BigDecimal.ZERO;
        powderTheo = null != weightData.getPowder1Theo() ? powderAct.add(weightData.getPowder1Theo()) : powderTheo;
        powderTheo = null != weightData.getPowder2Theo() ? powderAct.add(weightData.getPowder2Theo()) : powderTheo;
        powderTheo = null != weightData.getPowder3Theo() ? powderAct.add(weightData.getPowder3Theo()) : powderTheo;
        weightData.setPowderTheo(powderTheo);

        // 计算骨料、矿粉冷料用量
        BigDecimal gravelCold = BigDecimal.ZERO;
        gravelCold = null != weightData.getGravel1Cold() ? gravelAct.add(weightData.getGravel1Cold()) : gravelCold;
        gravelCold = null != weightData.getGravel2Cold() ? gravelAct.add(weightData.getGravel2Cold()) : gravelCold;
        gravelCold = null != weightData.getGravel3Cold() ? gravelAct.add(weightData.getGravel3Cold()) : gravelCold;
        gravelCold = null != weightData.getGravel4Cold() ? gravelAct.add(weightData.getGravel4Cold()) : gravelCold;
        gravelCold = null != weightData.getGravel5Cold() ? gravelAct.add(weightData.getGravel5Cold()) : gravelCold;
        gravelCold = null != weightData.getGravel6Cold() ? gravelAct.add(weightData.getGravel6Cold()) : gravelCold;
        weightData.setGravelCold(gravelCold);

        BigDecimal powderCold = BigDecimal.ZERO;
        powderCold = null != weightData.getPowder1Cold() ? powderAct.add(weightData.getPowder1Cold()) : powderCold;
        powderCold = null != weightData.getPowder2Cold() ? powderAct.add(weightData.getPowder2Cold()) : powderCold;
        powderCold = null != weightData.getPowder3Cold() ? powderAct.add(weightData.getPowder3Cold()) : powderCold;
        weightData.setPowderCold(powderCold);

        // 返回结果
        return weightData;
    }

    /**
     * @param ratio 使用占比
     * @param total 总量
     * @return 使用量
     * @Title countWeight
     * @Description 计算用量
     */
    private static BigDecimal countWeight(BigDecimal ratio, BigDecimal total) {
        if (null == ratio) {
            return BigDecimal.ZERO;
        }

        return ratio.multiply(total).divide(new BigDecimal(100), 5, RoundingMode.HALF_UP);
    }

}
